package com.example.administrator.x3picturetransliteration.Home.Activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.example.administrator.x3picturetransliteration.Base.BaseActivity;
import com.example.administrator.x3picturetransliteration.Base.BasePresenter;
import com.example.administrator.x3picturetransliteration.Home.Adapter.FeedbackAdapter;
import com.example.administrator.x3picturetransliteration.Home.Presenter.Activity.FeedbackPresenter;
import com.example.administrator.x3picturetransliteration.R;
import com.example.administrator.x3picturetransliteration.Util.ToastUtil;
import com.muzhi.camerasdk.model.CameraSdkParameterInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;

/**
 * 意见反馈
 * Created by Administrator on 2019/8/27.
 */

@RuntimePermissions
public class FeedbackActivity extends BaseActivity{

    @BindView(R.id.TitleText)TextView TitleText;
    @BindView(R.id.LButton)ImageView LButton;

    @BindView(R.id.listview)ListView listview;

    @BindView(R.id.contentEdit)EditText contentEdit; //意见反馈文字内容

    private List<String> list;
    private FeedbackAdapter mFeedbackAdapter;

    private FeedbackPresenter mFeedbackPresenter;

    @Override
    public int intiLayout() {
        return R.layout.home_activity_feedback;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TitleText.setText("意见反馈");
        LButton.setImageResource(R.mipmap.home_backw);

        mFeedbackPresenter = new FeedbackPresenter(this,mCompositeSubscriptions);

        list = new ArrayList<>();
        mFeedbackAdapter = new FeedbackAdapter(this,list);
        listview.setAdapter(mFeedbackAdapter);


    }

    /** 选择图片 */
    @OnClick(R.id.ChoiceView)
    public void ChoiceViewClick(){

        FeedbackActivityPermissionsDispatcher.getImgWithCheck(this,6);
    }

    /** 提交 */
    @OnClick(R.id.SubmissionText)
    public void SubmissionTextClick(){

        if ("".equals(contentEdit.getText() + "")){

            new ToastUtil(this).getToast(false,"请输入您的反馈意见").show();

        }else if ((contentEdit.getText() + "").length()>500){

            new ToastUtil(this).getToast(false,"文字内容不能大于500").show();

        }else if (list.size()<=0){

            new ToastUtil(this).getToast(false,"请选择上传图片").show();

        }else {

            add();
        }

    }

    /** 反馈记录 */
    @OnClick(R.id.FeedbackRecordText)
    public void FeedbackRecordTextClick(){

        startActivity(new Intent(this,FeedbackRecordActivity.class));
    }

    @OnClick(R.id.left_button)
    public void left_buttonClick(){

        finish();
    }

    /** 选择照片 */
    @NeedsPermission({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    public void getImg(final int max_image){

        CameraSdkParameterInfo mCameraSdkParameterInfo = new CameraSdkParameterInfo();
        mCameraSdkParameterInfo.setShow_camera(false);
        mCameraSdkParameterInfo.setFilter_image(true);
        mCameraSdkParameterInfo.setMax_image(max_image);
        Intent intent = new Intent();
        intent.setClassName(this, "com.muzhi.camerasdk.PhotoPickActivity");
        //intent.setClassName(getApplication(), "com.muzhi.camerasdk.CameraActivity");

        Bundle b = new Bundle();
        b.putSerializable(CameraSdkParameterInfo.EXTRA_PARAMETER, mCameraSdkParameterInfo);
        intent.putExtras(b);

        startActivityForResult(intent, CameraSdkParameterInfo.TAKE_PICTURE_FROM_GALLERY);

    }

    /** 上传图片 */
    private void add(){

        String imgs = null;

        for (String path:list)if (imgs==null) imgs = path; else imgs = imgs + "," + path;

        Map<String,String> map = new HashMap<>();

        map.put("content",contentEdit.getText() + "");
        map.put("imgs",imgs);

        mFeedbackPresenter.add(map, new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {

                contentEdit.setText("");
                list.clear();
                mFeedbackAdapter.notifyDataSetChanged();
            }
        });

    }

    /** 选择图片返回结果 */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == CameraSdkParameterInfo.TAKE_PICTURE_FROM_GALLERY) {
            if(data!=null){

                CameraSdkParameterInfo mCameraSdkParameterInfo=(CameraSdkParameterInfo)data.getExtras().getSerializable(CameraSdkParameterInfo.EXTRA_PARAMETER);

                mFeedbackPresenter.multiUpload(mCameraSdkParameterInfo.getImage_list(), new BasePresenter.Callback() {
                    @Override
                    public void getData(Object data) {

                        list.clear();
                        list.addAll(JSON.parseArray(data + "",String.class));
                        mFeedbackAdapter.notifyDataSetChanged();
                    }
                });

            }
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // NOTE: delegate the permission handling to generated method
        FeedbackActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }
}
